<?php

/**
 * Member form.
 *
 * @package    wildlive
 * @subpackage form
 * @author     Maximilian Lenkeit <dev@lenki.com>
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class MemberForm extends BaseMemberForm
{
  protected static $membershipFeesChoiceKey = 'MembershipFees';
  private $membershipFeeChoices = null;
  
  protected static $embeddedPersonalDataKey = 'PersonalData';
  protected static $embeddedOrganizationalDataKey = 'OrganizationalData';

  public function configure ()
  {
    parent::configure();
    
    // Embed Organizational Data
    $organizational_data = $this->getObject()
      ->getOrganizationalData();
    if (is_null($organizational_data))
      $organizational_data = new OrganizationalMemberData();
    $this->embedForm(self::$embeddedOrganizationalDataKey, new EmbeddedOrganizationalMemberDataForm($organizational_data, $this->getOptions()));
    
    // Embed Personal Data
    $personal_data = $this->getObject()
      ->getPersonalData();
    if (is_null($personal_data))
      $personal_data = new PersonalMemberData();
    $this->embedForm(self::$embeddedPersonalDataKey, new EmbeddedPersonalMemberDataForm($personal_data));
    
    // remove system fields
    unset($this['created_at'], $this['updated_at'], $this['deleted_at'], $this['created_by'], $this['updated_by'], $this['deleted_by'], 
        $this['slug']);
    unset($this['event_list']);
    
    // Gender field
    $this->widgetSchema['gender'] = new WidgetFormGenderChoice(array(
      'expanded' => false));
    $this->validatorSchema['gender'] = new sfValidatorChoice(array(
      'choices' => array(
      0 => 'male', 1 => 'female'), 'required' => true));
    
    // set labels
    $this->widgetSchema->setLabels(array(
      'first_name' => 'Vorname', 'last_name' => 'Nachname', 'gender' => 'Geschlecht'));
    
    // Error messages
    $this->adjustErrorMessageRequired('first_name');
    $this->adjustErrorMessageRequired('last_name');
    $this->adjustErrroMessageInvalid('first_name');
    $this->adjustErrroMessageInvalid('last_name');
  }

  public function bind (array $taintedValues = null, array $taintedFiles = null)
  {
    $this->addPreviouslyAddedPhoneNumbers($taintedValues);
    //    $this->addPreviouslyAddedAddresses ( $taintedValues );
    return parent::bind($taintedValues, $taintedFiles);
  }

  public function addPreviouslyAddedAddresses (array $taintedValues = null)
  {
    foreach ($taintedValues[self::$embeddedAddressesKey] as $key => $address)
    {
      if (! isset($this[self::$embeddedAddressesKey][$key]))
      {
        $this->addAddress($key);
      }
    }
  }

  public function addPreviouslyAddedPhoneNumbers (array $taintedValues = null)
  {
    $embedded_tainted_values = $taintedValues[self::$embeddedPersonalDataKey];
    $embedded_form = $this->getEmbeddedPersonalMemberDataForm();
    $embedded_form->addPreviouslyAddedPhoneNumbers($embedded_tainted_values);
    $this->reembedPersonalMemberDataForm($embedded_form);
  }

  /**
   * Add phone number form
   */
  public function addPhoneNumber ()
  {
    $embedded_form = $this->getEmbeddedPersonalMemberDataForm();
    $embedded_form->addPhoneNumber();
    // Re-embed form to apply changes
    $this->embedForm(self::$embeddedPersonalDataKey, $embedded_form);
  }

  public function addAddress ($num = null)
  {
    $form = $this->getEmbeddedForm(self::$embeddedAddressesKey);
    $form->addForm(null, $num);
    $this->embedForm(self::$embeddedAddressesKey, $form);
  }

  public function updateObjectEmbeddedForms ($values, $forms = null)
  {
    // Update PersonalMemberData object
    $embedded_personal_member_data_form = $this->getEmbeddedForm(self::$embeddedPersonalDataKey);
    $personal_member_data = $embedded_personal_member_data_form->getObject();
    $personal_member_data->setMember($this->getObject());
    
    // Update OrganizationalMemberData object
    $embedded_organizational_member_data_form = $this->getEmbeddedForm(self::$embeddedOrganizationalDataKey);
    $organizational_member_data = $embedded_organizational_member_data_form->getObject();
    $organizational_member_data->setMember($this->getObject());
    
    return parent::updateObjectEmbeddedForms($values, $forms);
  }

  public function saveEmbeddedForms ($con = null, $forms = null)
  {
    //		if($forms === null) {
    //			$contacts = $this->getValue(self::$embeddedPhoneContactsKey);
    //			$forms = $this->getEmbeddedForms();
    //			$dynamicForm = $this->getEmbeddedForm(self::$embeddedPhoneContactsKey);
    //			foreach($dynamicForm as $key => $form) {
    //				unset($forms[$key]);
    //			}
    //		} else
    if (is_array($forms))
    {
      foreach ($forms as $key => $form)
      {
//        if ($form instanceof EmbeddedPersonalMemberDataForm)
//        {
//          $form->getObject()
//            ->setMember($this->getObject());
//          $this->getObject()
//            ->setPersonalData($form->getObject());
//        }
      
   //        if ($form instanceof EmbeddedPhoneNumberForm) {
      //          if ($form->getObject ()->isModified ()) {
      //            $form->getObject ()->Member = $this->getObject ();
      //          } else {
      //            unset ( $forms [$key] );
      //          }
      //        } elseif ($form instanceof EmbeddedAddressForm) {
      //          if ($form->getObject ()->getStreet ()) { // TODO refactor, that's not nice but working *G*
      //            $form->getObject ()->BusinessPartner [] = $this->getObject ();
      //          } else {
      //            unset ( $forms [$key] );
      //          }
      //        }
      }
    }
    
    return parent::saveEmbeddedForms($con, $forms);
  }

  public function updateObject ($values = null)
  {
    //    if ($values === null)
    //      $values = $this->getValues ();
    //    
    //    $fees = $values [self::$membershipFeesChoiceKey];
    //    $fees = is_array ( $fees ) ? $fees : array ();
    //    $choices = $this->getMembershipFeeChoices ();
    //    foreach ( $choices as $year ) {
    //      if (in_array ( $year, $fees )) {
    //        //        $this->getObject ()->payMembershipFee ( $year );
    //      } else {
    //        //        $this->getObject ()->unpayMembershipFee ( $year );
    //      }
    //    }
    

    return parent::updateObject($values);
  }

  public function getMembershipFeeChoices ()
  {
    if (is_null($this->membershipFeeChoices))
    {
      $this->membershipFeeChoices = array();
      for ($i = date("Y") - 1; $i <= date("Y") + 1; $i ++)
      {
        $this->membershipFeeChoices[$i] = $i;
      }
    }
    return $this->membershipFeeChoices;
  }

  /**
   * Get embedded personal member data form
   * @return PersonalMemberDataForm
   */
  public function getEmbeddedPersonalMemberDataForm ()
  {
    // TODO wahat if null?
    return $this->getEmbeddedForm(self::$embeddedPersonalDataKey);
  }

  /**
   * Reembed personal member data form
   * @param PersonalMemberDataForm $form
   */
  protected function reembedPersonalMemberDataForm (PersonalMemberDataForm $form)
  {
    $this->embedForm(self::$embeddedPersonalDataKey, $form);
  }
}
